package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {
    // 获取OpenId
    public static final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session";

    // 获取AccessToken
    public static final String ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";
    // 获取手机号
    public static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;

    @Override
    public String getOpenId(String code) {

        // 1.定义参数
        Map<String, Object> dataMap = getConfigApi();
        dataMap.put("js_code", code);
        dataMap.put("grant_type", "authorization_code");

        // 2.执行 GET 请求
        String result = HttpUtil.get(OPENID_URL, dataMap);
        JSONObject jsonObject = JSONUtil.toBean(result, JSONObject.class);

        // 3.解析数据,获取OpenId
        // 3.1 errcode =0 表示成功 可以获取 openId
        String openid = jsonObject.getStr("openid");
        if (ObjectUtil.isEmpty(openid)) {
            throw new BaseException("获取OpenId失败");
        }
        return openid;

    }

    @NotNull
    private  Map<String, Object> getConfigApi() {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("appid", appId);
        dataMap.put("secret", appSecret);
        return dataMap;
    }

    @Override
    public String getPhone(String code) {
        // 1.定义参数
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("code", code);

        // 2.执行 POST 请求
        HttpResponse response = HttpUtil.createPost(PHONE_URL + getAccessToken())
                .body(JSONUtil.toJsonStr(dataMap), "application/json;charset=utf-8")
                .execute();
        // 3.解析数据,获取手机号
        String result = response.body();
        JSONObject jsonObject = JSONUtil.toBean(result,JSONObject.class);
        if (jsonObject.getInt("errcode") != 0) {
            throw new RuntimeException("获取手机号失败");
        }
        Object phone = jsonObject.getByPath( "phone_info.phoneNumber");
        return  phone.toString();
    }

    @Override
    public String getAccessToken() {
        // 1.定义参数
        HashMap<String,Object> dataMap = (HashMap<String, Object>) getConfigApi();
        dataMap.put("grant_type", "client_credential");


        // 2.执行 GET 请求
        String result = HttpUtil.get(ACCESS_TOKEN, dataMap);
        JSONObject jsonObject = JSONUtil.toBean(result, JSONObject.class);

        // 3.解析数据,获取AccessToken
        String accessToken = jsonObject.getStr("access_token");
        if (ObjectUtil.isEmpty(accessToken)) {
            throw new RuntimeException("获取AccessToken失败");
        }
        return accessToken;
    }
}
